package com.example.opengldemo._08_3D;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;

import com.example.opengldemo.R;
import com.example.opengldemo._08_3D.column.ColumnL;
import com.example.opengldemo._08_3D.column.Columu;
import com.example.opengldemo._09_OBJ.ObjDemo;
import com.example.opengldemo.util.LoadUtil;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyView3D extends GLSurfaceView implements GLSurfaceView.Renderer
{
    Columu columu ;
    Columu columu2 ;
    ColumnL columul ;
    ObjDemo objDemo;

    public MyView3D(Context context) {
        super(context);
        initConfig() ;
    }

    private void initConfig() {
        this.setEGLContextClientVersion(3);
        this.setRenderer(this);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0.5f  , 0.5f , 0.5f , 1.0f);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glEnable(GLES30.GL_CULL_FACE);
//        columu = new Columu(this);
//        columu2 = new Columu(this);
//        columul = new ColumnL(this) ;
        objDemo = LoadUtil.getLoadObj(this , "nine/ch.obj") ;
        MatrixState.setInitStack();
    }
    int texture2 ;
    int texture3 ;
    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport( 0 , 0 , i , i1);
        float ratio= (float) i / i1;
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 4f, 100);
        MatrixState.setCamera(0,0,7.2f,0f,0f,0f,0f,1.0f,0.0f);
        //GLES30.glEnable(GLES30.GL_CULL_FACE);
        MatrixState.setLightLocation(100,5,0);
        texture2 = TextureUtil.getTextureId(this , R.drawable.was1 , null, false) ;
        texture3 =  TextureUtil.getTextureId(this , R.drawable.was2, null, false) ;
//        columu.shaderHandle.setM_U_textureHandle(TextureUtil.getSafeTexureId(this , R.drawable.was)) ;
//        columul.shaderHandle.setM_U_textureHandle(TextureUtil.getSafeTexureId(this , R.drawable.was)) ;
//        columu2.shaderHandle.setM_U_textureHandle(TextureUtil.getSafeTexureId(this , R.drawable.was)) ;
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
        MatrixState.pushMatrix();
//        columu.drawSelf(columu.shaderHandle.get_U_textureHandle() , texture2 , texture3 );
//        columul.drawSelf(columu.shaderHandle.get_U_textureHandle() , texture2 , texture3 );
        MatrixState.translate(0 , 0 , 0.9f);
        objDemo.drawSelf( 0 , 0 , 0);
//        columu2.drawSelf(columu.shaderHandle.get_U_textureHandle() , texture2 , texture3 );
        MatrixState.popMatrix();
    }
}
